<template>
  <div class="safeCheck-detail">
    <secondTopPanel>
      <div slot='title'>编辑（隐患/意见）</div>
    </secondTopPanel>
    <evanForm ref="form" class="first-evan-form" :label-style="{width:'200upx',paddingRight:'20upx'}" :model='form' :rules='rules1'>
      <evanFormItem label='检查类型' prop="type">
        <uni-data-checkbox v-model="form.type" :localdata="range" @change="change"></uni-data-checkbox>
      </evanFormItem>
    </evanForm>
    <evanForm v-show="form.type==='0'" ref="form1" class="evan-form" :label-style="{width:'200upx',paddingRight:'20upx'}" :model='form' :rules='rules1'>
      <evanFormItem label='隐患名称' prop="title">
        <input class="form-input" v-model="form.title" type="text" placeholder="请输入隐患名称">
      </evanFormItem>
      <evanFormItem label='地点' prop="address">
        <input class="form-input" v-model="form.address" type="text" placeholder="请输入地点">
      </evanFormItem>
      <evanFormItem label='隐患类型' prop='dangerTypeParse' @click="openIndexPopup">
        <input class="form-input" v-model="form.dangerTypeParse" disabled type="text" placeholder="请选择">
      </evanFormItem>
      <evanFormItem label='检查情况' prop="dis">
        <input class="form-input" v-model="form.dis" placeholder="请输入检查情况">
      </evanFormItem>
      <evanFormItem label='照片上传' label-position='top' prop='fileUrls'>
        <ImageUpload :imgUrlData="form.fileUrlsParse" @imageUrlCallBack='imageUrlCallBack'></ImageUpload>
      </evanFormItem>
      <evanFormItem label='视频' label-position='top' prop='videoUrls'>
        <ImageUpload :imgUrlData="form.videoUrlsParse" :_mediaType="'video'" :_max="1" @imageUrlCallBack='videoUrlCallBack'></ImageUpload>
      </evanFormItem>
    </evanForm>
    <evanForm v-show="form.type==='1'" ref="form2" class="evan-form" :label-style="{width:'200upx',paddingRight:'20upx'}" :model='form' :rules='rules2'>
      <evanFormItem label='检查情况' prop="dis">
        <input class="form-input" v-model="form.dis" placeholder="请输入检查情况">
      </evanFormItem>
    </evanForm>
    <br>
    <button v-if="inspectState==='0'" type="primary" @click="submit">确认修改</button>
    <br>
    <!-- 隐患一级弹出框 -->
    <uni-popup ref="dangerDownPopup">
      <ly-tree @node-click='handleDanger' :props="indexProps" :tree-data="dangerDownTree" node-key="value">
      </ly-tree>
    </uni-popup>
  </div>
</template>

<script>
import uniDatetimePicker from '@/components/uni-datetime-picker/uni-datetime-picker.vue'
import ccPicker from '@/components/cc-picker/cc-picker.vue'
import LyTree from '@/components/ly-tree/ly-tree.vue'
import { getDeptList, getMyThreeDeptlist } from "@/api/user";
import secondTopPanel from '@/pages/component/top-panel/secondTopPanel.vue'
import evanForm from '@/components/evan-form/evan-form.vue'
import evanFormItem from '@/components/evan-form-item/evan-form-item.vue'
import uniDataCheckbox from '@/components/uni-data-checkbox/uni-data-checkbox.vue'
import ImageUpload from "@/pages/component/imageUpload/imageUpload.vue";
import { jobManagerInfoOne } from "@/api/jobManager";
import { rectifyInfoList } from "@/api/danger";
import { selectAssessmendocInfo, indexInfoList, indexTermList, depindexCutList, safetyCheckAdd, safeInspectResOne, safeInspectResEdit } from "@/api/safeCheck";
import moment from 'moment'
import JSONParse from "@/common/JSONParse.js";

export default {
  name: 'safeCheck-detail',
  components: { secondTopPanel, evanForm, evanFormItem, ImageUpload, LyTree, ccPicker, uniDatetimePicker, uniDataCheckbox },
  data () {
    return {
      range: [{ "value": '0', "text": "隐患" }, { "value": '1', "text": "意见" }],
      inspectState: '0',
      form: {
        inspectId: null,
        resCompanyId: null,
        title: null,
        address: null,
        content: null,
        dis: null,
        fileUrls: null,
        fileUrlsParse: null,
        videoUrls: null,
        videoUrlsParse: null,
        sourceType: "1",
        dangerType: null,
        safeCompanyName: null,
        dangerTypeParse: null,
        dangerType2: null,
      },
      fatherOption: null,
      indexProps: {
        label: 'label',
      },
      rules1: {
        title: [
          { required: true, message: '名字不能空' }
        ],
        dis: [
          { required: true, message: '描述不能空' }
        ],
      },
      rules2: {
        dis: [
          { required: true, message: '描述不能空' }
        ],
      }
    }
  },
  created () {
    this.dangerDownTree = JSONParse.hidden_danger_type
  },
  onLoad: function (option) {
    this.fatherOption = option
    this.inspectState = option.inspectState
    this.safeInspectResOne(option.id)
  },
  methods: {
    reset () {
      this.form = {
        inspectId: null,
        resCompanyId: null,
        safeCompanyId: null,
        safeCompanyName: null,
        title: null,
        address: null,
        content: null,
        dis: null,
        fileUrls: null,
        fileUrlsParse: null,
        videoUrls: null,
        videoUrlsParse: null,
        sourceType: "1",
        dangerType: null,
        type: '0',
        dangerTypeParse: null,
        dangerType2: null,
      }
    },
    // 隐患意见切换
    change (eg) {
      this.reset1()
      this.form.type = eg.detail.value
    },
    // 选中隐患类型节点
    handleDanger (obj) {
      if (obj.childNodesId.length === 0) {
        if (obj.parentId === '') {
          this.form.dangerType = obj.label
        } else {
          this.form.dangerTypeParse = obj.parentId + '/' + obj.label
          this.form.dangerType = obj.parentId
          this.form.dangerType2 = obj.label
        }
        this.$refs.dangerDownPopup.close()
      }
    },
    // 打开隐患类型弹窗
    openIndexPopup () {
      this.$refs.dangerDownPopup.open('bottom')
    },
    // 视频提交
    videoUrlCallBack (data) {
      this.form.videoUrls = JSON.stringify(data)
    },
    // 图片回调
    imageUrlCallBack (data) {
      this.form.fileUrls = JSON.stringify(data)
    },
    // 获取结果单条
    safeInspectResOne (id) {
      this.reset()
      safeInspectResOne({ id: id }).then(response => {
        for (var d in this.form) {
          this.form[d] = response.data[d]
        }
        this.form['id'] = id
        this.form['fileUrlsParse'] = JSON.parse(response.data['fileUrls'] || '[]')
        this.form['videoUrlsParse'] = JSON.parse(response.data['videoUrls'] || '[]')
        this.form['dangerTypeParse'] = `${this.form.dangerType || ''}${this.form.dangerType2 ? '/' : ''}${this.form.dangerType2 ? this.form.dangerType2 : ''}`
      })
    },
    reset1 () {
      this.form.title = null
      this.form.address = null
      this.form.dangerType = null
      this.form.dis = null
      this.form.fileUrls = null
      this.form.fileUrlsParse = null
      this.form.videoUrls = null
      this.form.videoUrlsParse = null
    },
    // 提交
    submit () {
      let init = 'form1'
      if (this.form?.type === '0') {
        init = 'form1'
      } else {
        init = 'form2'
      }
      this.$refs[init].validate((response) => {
        if (response) {
          safeInspectResEdit(this.form).then(res => {
            if (res.code === 200) {
              uni.showToast({
                title: '提交成功',
                icon: 'none',
                duration: 2000,
              })
              setTimeout(() => {
                uni.navigateBack()
              }, 1000);
            } else {
              uni.showToast({
                title: '提交失败',
                icon: 'none',
                duration: 2000,
              })
            }
          })
        }
      })

    }
  },
}
</script>

<style scoped lang="scss">
.safeCheck-detail {
  height: 100%;
  .first-evan-form {
    overflow: auto;
    padding: 0 40upx;
    margin-top: 0upx;
    background-color: #fff;
    padding-top: 150upx;
  }
  .evan-form {
    overflow: auto;
    padding: 0 40upx;
    margin-top: 0upx;
    background-color: #fff;
  }
  .btn {
    margin-top: 40upx;
    display: flex;
    .submit {
      width: 90%;
      line-height: 80upx;
    }
    .temp-save {
      width: 40%;
      line-height: 80upx;
    }
  }
}
.fileBtn {
  color: #009fdf;
  padding-left: 10upx;
}

/deep/ .ly-tree {
  height: 800upx;
  overflow: auto;
  z-index: 9999;
}
/deep/ .form-input {
  width: 100%;
}
/deep/ .ly-tree-node {
  position: relative;
  white-space: normal;
  padding: 5px 0;
}
/deep/ .ly-tree-node__content {
  height: auto;
}
</style>
